package cz.data.common.mybatis.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import cz.data.common.mybatis.aspectj.DataScopeAspect;
import cz.data.common.mybatis.injector.DataLogicSqlInjector;
import cz.data.common.mybatis.interceptor.DataScopeInterceptor;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@AutoConfigureOrder(-1)
@EnableTransactionManagement
public class DataBatisPlusConfig {

	/**
	 * 分页插件
	 */
	@Bean
	public MybatisPlusInterceptor mybatisPlusInterceptor() {
		PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
		// 设置最大单页限制数量，默认 500 条，-1 不受限制
		paginationInnerInterceptor.setMaxLimit(-1L);
		// 分页合理化
		paginationInnerInterceptor.setOverflow(true);
		MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
		// 分页插件

		interceptor.addInnerInterceptor(paginationInnerInterceptor);
		return interceptor;
	}
	/**
	 * 数据权限插件
	 *
	 * @return DataScopeInterceptor
	 */
	@Bean
	public DataScopeInterceptor dataScopeInterceptor() {
		return new DataScopeInterceptor();
	}

	/**
	 * 数据过滤处理（基于注解式）
	 *
	 * @return dataScopeAspect
	 */
	@Bean
	public DataScopeAspect dataScopeAspect() {
		return new DataScopeAspect();
	}

	/**
	 * 自定义 SqlInjector
	 * 里面包含自定义的全局方法
	 */
	@Bean
	public DataLogicSqlInjector myLogicSqlInjector() {
		return new DataLogicSqlInjector();
	}

	@Bean
	public DataMetaObjectHandler dataMetaObjectHandler() {
		return new DataMetaObjectHandler();
	}
}
